package com.jhgsys.internal.incentive.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.jhgsys.internal.base.entity.BaseGoods;
import com.jhgsys.internal.base.service.IBaseGoodsService;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.exception.FebsException;
import com.jhgsys.internal.common.utils.BeanHelper;
import com.jhgsys.internal.common.utils.DateUtil;
import com.jhgsys.internal.incentive.constant.IncentiveRewardConstant;
import com.jhgsys.internal.incentive.dto.IncentiveGoodsOrderDtlDto;
import com.jhgsys.internal.incentive.dto.IncentiveGoodsOrderDto;
import com.jhgsys.internal.incentive.dto.IncentiveGoodsOrderUserDto;
import com.jhgsys.internal.incentive.dto.IncentiveRewardApplyDto;
import com.jhgsys.internal.incentive.entity.*;
import com.jhgsys.internal.incentive.mapper.IncentiveRewardApplyMapper;
import com.jhgsys.internal.incentive.service.*;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.utils.FebsUtil;
import com.jhgsys.internal.system.entity.Tenant;
import com.jhgsys.internal.system.entity.User;
import com.jhgsys.internal.system.service.ITenantService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 安全奖励申请 Service实现
 *
 * @author wanghjw
 * @date 2024-09-03 14:08:36
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class IncentiveRewardApplyServiceImpl extends ServiceImpl<IncentiveRewardApplyMapper, IncentiveRewardApply> implements IIncentiveRewardApplyService {

    @Autowired
    private IIncentiveRewardFileService incentiveRewardFileService;

    @Autowired
    private IIncentiveRewardGoodsService incentiveRewardGoodsService;

    @Autowired
    private IIncentiveRewardUserService incentiveRewardUserService;

    @Autowired
    private ITenantService tenantService;


    @Autowired
    private IIncentiveRewardAuditRecordService incentiveRewardAuditRecordService;


    @Autowired
    private IBaseGoodsService iBaseGoodsService;

    @Autowired
    private IIncentiveGoodsOrderService incentiveGoodsOrderService;
    /**
     * 查询（分页）
     * @author wanghjw
     * @date 2024-09-03 14:08:36
     * @param request QueryRequest
     * @param incentiveRewardApply incentiveRewardApply
     * @return IPage<IncentiveRewardApply>
    */
    @Override
    public IPage<IncentiveRewardApplyDto> findIncentiveRewardApplyPage(QueryRequest request, IncentiveRewardApplyDto incentiveRewardApply) {
        LambdaQueryWrapper<IncentiveRewardApply> queryWrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(incentiveRewardApply.getDeletedFlg())){
            queryWrapper.eq(IncentiveRewardApply::getDeletedFlg, incentiveRewardApply.getDeletedFlg());
        }else {
            queryWrapper.eq(IncentiveRewardApply::getDeletedFlg, FebsConstant.VALID);
        }

        if(StringUtils.isNotBlank(incentiveRewardApply.getTenantId())){
            queryWrapper.eq(IncentiveRewardApply::getTenantId, incentiveRewardApply.getTenantId());
        }
        if(StringUtils.isNotBlank(incentiveRewardApply.getRewardType())){
            queryWrapper.eq(IncentiveRewardApply::getRewardType, incentiveRewardApply.getRewardType());
        }

        if(StringUtils.isNotBlank(incentiveRewardApply.getApplyUserId())){
            queryWrapper.eq(IncentiveRewardApply::getApplyUserId, incentiveRewardApply.getApplyUserId());
        }

        if(StringUtils.isNotBlank(incentiveRewardApply.getStartDate()) && StringUtils.isNotBlank(incentiveRewardApply.getEndDate())){
            queryWrapper.between(IncentiveRewardApply::getCreatedTime
                    , DateUtil.parseDate(incentiveRewardApply.getStartDate()+DateUtil.START_DATA_PATTERN,DateUtil.FULL_TIME_SPLIT_PATTERN)
                    , DateUtil.parseDate(incentiveRewardApply.getEndDate()+DateUtil.END_DATA_PATTERN,DateUtil.FULL_TIME_SPLIT_PATTERN));
        }

        if(StringUtils.isNotBlank(incentiveRewardApply.getStatus())){
            if(incentiveRewardApply.getStatus().indexOf(StringPool.COMMA)>-1){
                List<String> list = Arrays.asList(incentiveRewardApply.getStatus().split(StringPool.COMMA));
                queryWrapper.in(IncentiveRewardApply::getStatus,list);
            }else {
                queryWrapper.eq(IncentiveRewardApply::getStatus, incentiveRewardApply.getStatus());
            }

        }

        if(StringUtils.isNotBlank(incentiveRewardApply.getYear())){
            Date startDate = DateUtil.getYuanDan(incentiveRewardApply.getYear());
            Date endDate = DateUtil.getYearLastTime(incentiveRewardApply.getYear());
            queryWrapper.between(IncentiveRewardApply::getApplyData, startDate,endDate);
        }

        if(StringUtils.isNotBlank(incentiveRewardApply.getRewardUserName())){
            IncentiveRewardUser queryUser = new IncentiveRewardUser();
            queryUser.setRealName(incentiveRewardApply.getRewardUserName());
            List<IncentiveRewardUser> userList = incentiveRewardUserService.findIncentiveRewardUserList(queryUser);
            if(!CollectionUtils.isEmpty(userList)){
                List<String> idList = userList.stream().map(IncentiveRewardUser::getRewardApplyId).collect(Collectors.toList());
                queryWrapper.in(IncentiveRewardApply::getRewardApplyId,idList);
            }else {
                return new Page<IncentiveRewardApplyDto>();
            }
        }
        queryWrapper.orderByDesc(IncentiveRewardApply::getCreatedTime);
        Page<IncentiveRewardApply> page = new Page<>(request.getPageNum(), request.getPageSize());
        FebsUtil.pageWrapper(page, request);

        IPage<IncentiveRewardApply> applyIPage = this.page(page, queryWrapper);
        List<IncentiveRewardApply> applyList = applyIPage.getRecords();
        List<IncentiveRewardApplyDto> dtoList = BeanHelper.copyWithCollection(applyList,IncentiveRewardApplyDto.class);
        IPage<IncentiveRewardApplyDto> dtoIPage = new Page<>();
        BeanHelper.copyProperties(applyIPage,IncentiveRewardApplyDto.class);
        if(!CollectionUtils.isEmpty(dtoList)){
            String applyIdListStr = dtoList.stream().map(IncentiveRewardApply::getRewardApplyId).collect(Collectors.joining(StringPool.COMMA));

            IncentiveRewardUser queryUser = new IncentiveRewardUser();
            queryUser.setRewardApplyId(applyIdListStr);
            List<IncentiveRewardUser> userList = incentiveRewardUserService.findIncentiveRewardUserList(queryUser);

            IncentiveRewardGoods queryGood = new IncentiveRewardGoods();
            queryGood.setRewardApplyId(applyIdListStr);
            List<IncentiveRewardGoods> goodsList = incentiveRewardGoodsService.findIncentiveRewardGoodsList(queryGood);

            dtoList.forEach(item->{
                List<IncentiveRewardUser> resultUser = userList.stream().filter(i->i.getRewardApplyId().equals(item.getRewardApplyId())).collect(Collectors.toList());
                item.setUserList(resultUser);

                List<IncentiveRewardGoods> resultGood = goodsList.stream().filter(i->i.getRewardApplyId().equals(item.getRewardApplyId())).collect(Collectors.toList());
                item.setGoodsList(resultGood);
            });

            dtoIPage.setRecords(dtoList);
            dtoIPage.setTotal(applyIPage.getTotal());
            dtoIPage.setCurrent(applyIPage.getCurrent());
        }
        return dtoIPage;
    }

    /**
     * 查询（所有）
     * @author wanghjw
     * @date 2024-09-03 14:08:36
     * @param incentiveRewardApply incentiveRewardApply
     * @return List<IncentiveRewardApply>
     */
    @Override
    public List<IncentiveRewardApply> findIncentiveRewardApplyList(IncentiveRewardApply incentiveRewardApply) {
	    LambdaQueryWrapper<IncentiveRewardApply> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(IncentiveRewardApply::getDeletedFlg, 0);
		// TODO 设置查询条件
		return this.baseMapper.selectList(queryWrapper);
    }

    /**
    * 根据ID查询
    * @author wanghjw
    * @date 2024-09-03 14:08:36
    * @param id
    * @return IncentiveRewardApply
    */
    @Override
    public IncentiveRewardApplyDto findIncentiveRewardApplyById(String id){
        IncentiveRewardApply apply = this.baseMapper.selectById(id);
        IncentiveRewardApplyDto dto = new IncentiveRewardApplyDto();
        if(ObjectUtils.isNotEmpty(apply)){
            BeanUtils.copyProperties(apply,dto);
            IncentiveRewardUser queryUser = new IncentiveRewardUser();
            queryUser.setRewardApplyId(apply.getRewardApplyId());
            List<IncentiveRewardUser> userList = incentiveRewardUserService.findIncentiveRewardUserList(queryUser);
            dto.setUserList(userList);

            if(IncentiveRewardConstant.IS_OR_NO.IS.equals(apply.getRewardGoodsFlg())){
                IncentiveRewardGoods queryGood = new IncentiveRewardGoods();
                queryGood.setRewardApplyId(apply.getRewardApplyId());
                List<IncentiveRewardGoods> goodsList = incentiveRewardGoodsService.findIncentiveRewardGoodsList(queryGood);
                dto.setGoodsList(goodsList);
            }

            IncentiveRewardFile queryFile = new IncentiveRewardFile();
            queryFile.setRewardApplyId(apply.getRewardApplyId());
            queryFile.setRewardFileType(IncentiveRewardConstant.REWARD_FILE_TYPE.APPLY_FILE);
            List<IncentiveRewardFile> fileList = incentiveRewardFileService.findIncentiveRewardFileList(queryFile);
            dto.setFileList(fileList);
        }
        return dto;
    }

    /**
     * 新增
     * @author wanghjw
     * @date 2024-09-03 14:08:36
     * @param incentiveRewardApply incentiveRewardApply
     */
    @Override
    @Transactional
    public void createIncentiveRewardApply(IncentiveRewardApplyDto incentiveRewardApply) {
        incentiveRewardApply.setCreatedTime(new Date());
        this.save(incentiveRewardApply);
        List<IncentiveRewardGoods> goddsList = incentiveRewardApply.getGoodsList();
        if(!CollectionUtils.isEmpty(goddsList)){
            goddsList.forEach(item ->{
                item.setCreatedBy(incentiveRewardApply.getCreatedBy());
                item.setCreatedTime(incentiveRewardApply.getCreatedTime());
                item.setTenantId(incentiveRewardApply.getTenantId());
                item.setDeletedBy(FebsConstant.VALID);
                item.setRewardApplyId(incentiveRewardApply.getRewardApplyId());
            });
            incentiveRewardGoodsService.saveBatch(goddsList);
        }
        List<IncentiveRewardUser> userList = incentiveRewardApply.getUserList();
        if(!CollectionUtils.isEmpty(userList)){
            userList.forEach(item->{
                item.setCreatedBy(incentiveRewardApply.getCreatedBy());
                item.setCreatedTime(incentiveRewardApply.getCreatedTime());
                item.setTenantId(incentiveRewardApply.getTenantId());
                item.setDeletedBy(FebsConstant.VALID);
                item.setRewardApplyId(incentiveRewardApply.getRewardApplyId());
            });
            incentiveRewardUserService.saveBatch(userList);
        }

        List<IncentiveRewardFile> fileList = incentiveRewardApply.getFileList();
        if(!CollectionUtils.isEmpty(fileList)){
            fileList.forEach(item->{
                item.setCreatedBy(incentiveRewardApply.getCreatedBy());
                item.setCreatedTime(incentiveRewardApply.getCreatedTime());
                item.setTenantId(incentiveRewardApply.getTenantId());
                item.setDeletedBy(FebsConstant.VALID);
                item.setRewardApplyId(incentiveRewardApply.getRewardApplyId());
                item.setRewardFileType(IncentiveRewardConstant.REWARD_FILE_TYPE.APPLY_FILE);
            });
            incentiveRewardFileService.saveBatch(fileList);
        }
    }

    /**
     * 修改
     * @author wanghjw
     * @date 2024-09-03 14:08:36
     * @param incentiveRewardApply incentiveRewardApply
     */
    @Override
    @Transactional
    public void updateIncentiveRewardApply(IncentiveRewardApplyDto incentiveRewardApply) {
        //更新申请表
        incentiveRewardApply.setUpdatedTime(new Date());
        this.saveOrUpdate(incentiveRewardApply);
        //把上个批次人置为失效
        incentiveRewardUserService.update(new LambdaUpdateWrapper<IncentiveRewardUser>()
                .eq(IncentiveRewardUser::getRewardApplyId,incentiveRewardApply.getRewardApplyId())
                .eq(IncentiveRewardUser::getDeletedFlg,FebsConstant.VALID)
                .set(IncentiveRewardUser::getDeletedFlg,FebsConstant.INVALID)
                .set(IncentiveRewardUser::getDeletedBy,incentiveRewardApply.getUpdatedBy())
                .set(IncentiveRewardUser::getDeletedTime,incentiveRewardApply.getUpdatedTime())
        );
        //把上个批次商品置为失效
        incentiveRewardGoodsService.update(new LambdaUpdateWrapper<IncentiveRewardGoods>()
                .eq(IncentiveRewardGoods::getRewardApplyId,incentiveRewardApply.getRewardApplyId())
                .eq(IncentiveRewardGoods::getDeletedFlg,FebsConstant.VALID)
                .set(IncentiveRewardGoods::getDeletedFlg,FebsConstant.INVALID)
                .set(IncentiveRewardGoods::getDeletedBy,incentiveRewardApply.getUpdatedBy())
                .set(IncentiveRewardGoods::getDeletedTime,incentiveRewardApply.getUpdatedTime())
        );
        //把上个批次上传文件为失效
        incentiveRewardFileService.update(new LambdaUpdateWrapper<IncentiveRewardFile>()
                        .eq(IncentiveRewardFile::getRewardApplyId,incentiveRewardApply.getRewardApplyId())
                        .eq(IncentiveRewardFile::getDeletedFlg,FebsConstant.VALID)
                        .eq(IncentiveRewardFile::getRewardFileType,IncentiveRewardConstant.REWARD_FILE_TYPE.APPLY_FILE)
                        .set(IncentiveRewardFile::getDeletedFlg,FebsConstant.INVALID)
                        .set(IncentiveRewardFile::getDeletedBy,incentiveRewardApply.getUpdatedBy())
                        .set(IncentiveRewardFile::getDeletedTime,incentiveRewardApply.getUpdatedTime())
        );
        //批量新增商品
        List<IncentiveRewardGoods> goddsList = incentiveRewardApply.getGoodsList();
        if(!CollectionUtils.isEmpty(goddsList)){
            goddsList.forEach(item ->{
                item.setCreatedBy(incentiveRewardApply.getCreatedBy());
                item.setCreatedTime(incentiveRewardApply.getCreatedTime());
                item.setTenantId(incentiveRewardApply.getTenantId());
                item.setDeletedBy(FebsConstant.VALID);
                item.setRewardApplyId(incentiveRewardApply.getRewardApplyId());
            });
            incentiveRewardGoodsService.saveBatch(goddsList);
        }
        //批量新增人员
        List<IncentiveRewardUser> userList = incentiveRewardApply.getUserList();
        if(!CollectionUtils.isEmpty(userList)){
            userList.forEach(item->{
                item.setCreatedBy(incentiveRewardApply.getCreatedBy());
                item.setCreatedTime(incentiveRewardApply.getCreatedTime());
                item.setTenantId(incentiveRewardApply.getTenantId());
                item.setDeletedBy(FebsConstant.VALID);
                item.setRewardApplyId(incentiveRewardApply.getRewardApplyId());
            });
            incentiveRewardUserService.saveBatch(userList);
        }
        //批量上传奖励文件
        List<IncentiveRewardFile> fileList = incentiveRewardApply.getFileList();
        if(!CollectionUtils.isEmpty(fileList)){
            fileList.forEach(item->{
                item.setCreatedBy(incentiveRewardApply.getCreatedBy());
                item.setCreatedTime(incentiveRewardApply.getCreatedTime());
                item.setTenantId(incentiveRewardApply.getTenantId());
                item.setDeletedBy(FebsConstant.VALID);
                item.setRewardApplyId(incentiveRewardApply.getRewardApplyId());
                item.setRewardFileType(IncentiveRewardConstant.REWARD_FILE_TYPE.APPLY_FILE);
            });
            incentiveRewardFileService.saveBatch(fileList);
        }
    }

    /**
     * 逻辑删除
     * @author wanghjw
     * @date 2024-09-03 14:08:36
     * @param incentiveRewardApply incentiveRewardApply
     * @param incentiveRewardApplyIds incentiveRewardApplyIds
     */
    @Override
    @Transactional
    public void deleteIncentiveRewardApply(IncentiveRewardApply incentiveRewardApply,String incentiveRewardApplyIds) {
        List<String> list = Arrays.asList(incentiveRewardApplyIds.split(StringPool.COMMA));
        this.baseMapper.update(incentiveRewardApply,new LambdaQueryWrapper<IncentiveRewardApply>().in(IncentiveRewardApply::getRewardApplyId,list));
	}



    /**
    * 物理删除
    * @author wanghjw
    * @date 2024-09-03 14:08:36
    * @param incentiveRewardApplyIds incentiveRewardApplyIds
    */
    @Override
    @Transactional
    public void physicallyDeleteIncentiveRewardApply(String incentiveRewardApplyIds) {
        List<String> list = Arrays.asList(incentiveRewardApplyIds.split(StringPool.COMMA));
        LambdaQueryWrapper<IncentiveRewardApply> wapper = new LambdaQueryWrapper<>();
        wapper.in(IncentiveRewardApply::getRewardApplyId,list);
        this.remove(wapper);
    }

    /**
     * 获取申请编号
     * @param tenantId
     * @return
     */
    @Override
    public String createIncentiveRewardApplyNo(String tenantId) {
        String no = "";
        String tenantCode = "";
        Tenant tenant = tenantService.getById(tenantId);
        if(null != tenant){
            tenantCode = tenant.getTenantCode();
        }
        IncentiveRewardApply apply = this.baseMapper.selectOne(
                new LambdaQueryWrapper<IncentiveRewardApply>().eq(IncentiveRewardApply::getTenantId,tenantId)
                        .orderByDesc(IncentiveRewardApply::getCreatedTime).last("limit 1"));
        no = Calendar.getInstance().get(Calendar.YEAR) + tenantCode;
        if(null != apply){
            //在最新的编号上+1
            String currentNewestNo = apply.getRewardApplyNo().substring(apply.getRewardApplyNo().length()-5,apply.getRewardApplyNo().length());
            no += String.format("%05d",Integer.parseInt(currentNewestNo)+1);
        }else{
            //创建该公司下第一个事故编号
            no += IncentiveRewardConstant.APPLY_NO_START_NUM;
        }
        return no;
    }

    /**
     * 审核
     *
     * @param incentiveRewardApply
     * @param oriStatus            审核前状态
     * @param currentUser
     */
    @Override
    @Transactional
    public void auditIncentiveRewardApply(IncentiveRewardApply incentiveRewardApply, String oriStatus, User currentUser) throws FebsException {
        int updateRow = this.baseMapper.update(incentiveRewardApply,new LambdaUpdateWrapper<IncentiveRewardApply>()
                .eq(IncentiveRewardApply::getRewardApplyId,incentiveRewardApply.getRewardApplyId())
                .eq(IncentiveRewardApply::getDeletedFlg,FebsConstant.VALID)
                .eq(IncentiveRewardApply::getStatus,oriStatus));
        if(updateRow == 1){
            IncentiveRewardAuditRecord record = new IncentiveRewardAuditRecord();
            record.setCreatedBy(incentiveRewardApply.getUpdatedBy());
            record.setDeletedFlg(FebsConstant.VALID);
            if(IncentiveRewardConstant.APPLY_STATUS.WAIT_ISSUANCE.equals(incentiveRewardApply.getStatus())){
                record.setAuditResult(IncentiveRewardConstant.IS_OR_NO.IS);
            }else {
                record.setAuditResult(IncentiveRewardConstant.IS_OR_NO.NO);
            }
            record.setAuditReason(incentiveRewardApply.getAuditRemark());
            record.setAuditRealName(incentiveRewardApply.getAuditRealName());
            record.setQuotaApplyId(incentiveRewardApply.getRewardApplyId());
            record.setAuditUserId(incentiveRewardApply.getAuditUserId());
            record.setTenantId(incentiveRewardApply.getTenantId());
            this.incentiveRewardAuditRecordService.createIncentiveRewardAuditRecord(record);


            if(IncentiveRewardConstant.APPLY_STATUS.WAIT_ISSUANCE.equals(incentiveRewardApply.getStatus())){
                saveIncentiveGoodsOrder(incentiveRewardApply.getRewardApplyId(),currentUser);
            }
        }
    }

    /**
     * @param applyId
     * @param currentUser
     */
    @Transactional
    public void saveIncentiveGoodsOrder(String applyId, User currentUser) throws FebsException {
        IncentiveRewardApply incentiveRewardApply = this.baseMapper.selectById(applyId);
        if(IncentiveRewardConstant.IS_OR_NO.NO.equals(incentiveRewardApply.getRewardGoodsFlg())){
            return;
        }
        IncentiveGoodsOrderDto order = new IncentiveGoodsOrderDto();
        order.setTenantId(incentiveRewardApply.getTenantId());
        order.setGoodsOrderType(IncentiveRewardConstant.GOODS_ORDER_TYPE.PEACE);
        order.setRewardApplyId(incentiveRewardApply.getRewardApplyId());
        order.setOrderScore(0);
//        order.setOrderNo(incentiveRewardApply.getRewardApplyNo());
        order.setOrderDate(new Date());
        byte[] bytes = incentiveRewardApply.getRewardApplyNo().getBytes(StandardCharsets.UTF_8);
        UUID uuid = UUID.nameUUIDFromBytes(bytes);
        order.setExchangeCode(uuid.toString().replaceAll("-", ""));
        order.setExchangeDate(new Date());
        order.setStatus(IncentiveRewardConstant.ORDER_STATUS.WAIT_SUBMIT);
        order.setCreatedBy(incentiveRewardApply.getUpdatedBy());
        order.setCreatedTime(new Date());


        List<IncentiveGoodsOrderDtlDto> goodsOrderList = new ArrayList<>();

        IncentiveRewardGoods queryGood = new IncentiveRewardGoods();
        queryGood.setRewardApplyId(incentiveRewardApply.getRewardApplyId());
        List<IncentiveRewardGoods> incentiveRewardGoodsList = incentiveRewardGoodsService.findIncentiveRewardGoodsList(queryGood);


        String incentiveRewardGoodsIds = incentiveRewardGoodsList.stream().map(IncentiveRewardGoods::getGoodsId).collect(Collectors.joining(StringPool.COMMA));
        BaseGoods goods = new BaseGoods();
        goods.setGoodsId(incentiveRewardGoodsIds);
        List<BaseGoods> baseGoodsList = iBaseGoodsService.findBaseGoodsList(goods);
        if(!CollectionUtils.isEmpty(baseGoodsList)){
            baseGoodsList.forEach(item->{
                IncentiveGoodsOrderDtlDto  goodsOrderDtlDto = new IncentiveGoodsOrderDtlDto();
                goodsOrderDtlDto.setTenantId(incentiveRewardApply.getTenantId());
                goodsOrderDtlDto.setGoodsId(item.getGoodsId());
                goodsOrderDtlDto.setGoodsName(item.getGoodsName());
                goodsOrderDtlDto.setGoodsPrice(item.getGoodsPrice());
                goodsOrderDtlDto.setGoodsUnit(item.getGoodsUnit());
                goodsOrderDtlDto.setExchangeScore(new BigDecimal(item.getExchangeScore()));
                goodsOrderDtlDto.setGoodsImage(item.getSmallPath());
                goodsOrderDtlDto.setGoodsNumber(incentiveRewardGoodsList.stream()
                        .filter(i->item.getGoodsId().equals(i.getGoodsId()))
                        .findFirst()
                        .get()
                        .getGoodsNumber());
                goodsOrderDtlDto.setCreatedBy(incentiveRewardApply.getUpdatedBy());
                goodsOrderDtlDto.setCreatedTime(new Date());
                goodsOrderDtlDto.setDeletedFlg(FebsConstant.VALID);
                goodsOrderList.add(goodsOrderDtlDto);
            });
            order.setGoodsList(goodsOrderList);
        }


        List<IncentiveGoodsOrderUser> goodsUserList = new ArrayList<>();

        IncentiveRewardUser queryUser = new IncentiveRewardUser();
        queryUser.setRewardApplyId(incentiveRewardApply.getRewardApplyId());
        List<IncentiveRewardUser> userList = incentiveRewardUserService.findIncentiveRewardUserList(queryUser);
        if(!CollectionUtils.isEmpty(userList)){
            userList.forEach(item->{
                IncentiveGoodsOrderUser goodsOrderUser = new IncentiveGoodsOrderUser();
                goodsOrderUser.setTenantId(incentiveRewardApply.getTenantId());
                goodsOrderUser.setUserId(item.getUserId());
                goodsOrderUser.setUserNo(item.getUserNo());
                goodsOrderUser.setRealName(item.getRealName());
                goodsOrderUser.setDeptId(item.getDeptId());
                goodsOrderUser.setDeptName(item.getDeptName());
                goodsOrderUser.setCreatedBy(incentiveRewardApply.getUpdatedBy());
                goodsOrderUser.setCreatedTime(new Date());
                goodsOrderUser.setDeletedFlg(FebsConstant.VALID);
                goodsUserList.add(goodsOrderUser);
            });
            List<IncentiveGoodsOrderUserDto> goodsOrderUserDtoList = BeanHelper.copyWithCollection(goodsUserList, IncentiveGoodsOrderUserDto.class);
            order.setUserList(goodsOrderUserDtoList);
        }
        order.setUser(currentUser);
        incentiveGoodsOrderService.createPeaceIncentiveGoodsOrder(order);

    }

    /**
     * 签发
     * @param incentiveRewardApply
     * @param oriStatus 签发前状态
     */
    @Override
    @Transactional
    public void issuanceIncentiveRewardApply(IncentiveRewardApply incentiveRewardApply,String oriStatus) {
        int updateRow = this.baseMapper.update(incentiveRewardApply,new LambdaUpdateWrapper<IncentiveRewardApply>()
                .eq(IncentiveRewardApply::getRewardApplyId,incentiveRewardApply.getRewardApplyId())
                .eq(IncentiveRewardApply::getDeletedFlg,FebsConstant.VALID)
                .eq(IncentiveRewardApply::getStatus,oriStatus));
    }

    @Override
    @Transactional
    public void receiveIncentiveRewardApply(IncentiveRewardApplyDto incentiveRewardApply, IncentiveGoodsOrder incentiveGoodsOrder) {
        //签收励文件
        List<IncentiveRewardFile> fileList = incentiveRewardApply.getReceiveFileList();
        if(!CollectionUtils.isEmpty(fileList)){
            fileList.forEach(item->{
                item.setCreatedBy(incentiveRewardApply.getCreatedBy());
                item.setCreatedTime(incentiveRewardApply.getCreatedTime());
                item.setTenantId(incentiveRewardApply.getTenantId());
                item.setDeletedBy(FebsConstant.VALID);
                item.setRewardApplyId(incentiveRewardApply.getRewardApplyId());
                item.setRewardFileType(IncentiveRewardConstant.REWARD_FILE_TYPE.ISSUANCE_FILE);
            });
            incentiveRewardFileService.saveBatch(fileList);
        }
        incentiveGoodsOrder.setStatus(IncentiveRewardConstant.ORDER_STATUS.RECEIVED);
        incentiveGoodsOrder.setUpdatedTime(new Date());
        incentiveGoodsOrderService.updateIncentiveGoodsOrder(incentiveGoodsOrder);
    }


}
